package com.lxw.oa.util;
import java.math.BigDecimal;
import java.util.List;
import com.lxw.oa.entity.Menu;


public class BackMenuUtil {
    public  int index=1;
    public List<Menu> tmpMenus;
    public String getBackMenuByMenulist(){
        String resultS = "";
        //添加第一层菜单按钮
        for (Menu menu: tmpMenus) {
            if(menu.getMenu_level().equals("1")){    //level是1为最外层
                String childS = getBackMenuChildEntityByPID(Integer.toString(menu.getId()));
                if(childS==null||childS.equals("")){    //无子类
                    index++;
                    resultS += "<li><a href='javascript:void(0)' onclick=menuItem('"+ menu.getMenu_url() +"','"+menu.getMenu_name()+"','"+index+"')  class='myMenus' ><i class=\"fa "
                            + menu.getMenu_icon()+ "\"></i><span> "
                            + menu.getMenu_name() + "</a></span></li>";
                }else{
                    if("/".equals(menu.getMenu_url())){
                        resultS += "<li  class=\"treeview\">\r\n"
                                + "<a href=\"javascript:void(0)\">"
                                + "<i class=\"fa "
                                + menu.getMenu_icon() +"\"></i> <span>"
                                + menu.getMenu_name()  + "</span> <i class=\"fa fa-angle-left pull-right\"></i>"
                                + "</a>\r\n"
                                + "<ul class=\"treeview-menu\">"
                                + getBackMenuChildEntityByPID(Integer.toString(menu.getId()))
                                + "</ul>";
                    }else{
                        index++;
                        resultS += "<li  class=\"treeview\">\r\n"
                                + "<a href='javascript:void(0)' onclick=menuItem('"+ menu.getMenu_url() +"','"+menu.getMenu_name()+"','"+index+"')  class='myMenus'>"
                                + "<i class=\"fa "
                                + menu.getMenu_icon() +"\"></i> <span>"
                                + menu.getMenu_name()  + "</span> <i class=\"fa fa-angle-left pull-right\"></i>"
                                + "</a>\r\n"
                                + "<ul class=\"treeview-menu\">"
                                + getBackMenuChildEntityByPID(Integer.toString(menu.getId()))
                                + "</ul>";
                    }
                }
            }
        }
        return resultS;
    }

    //添加第二层以及子菜单
    public String getBackMenuChildEntityByPID(String id){
        String results = "";
        for(Menu menu: tmpMenus){
            //遍历菜单列表，如果有此菜单的子菜单则拼接
            if(id.equals(menu.getParent_id())){
                String childS = getBackMenuChildEntityByPID(Integer.toString(menu.getId()));
                if(childS==null||childS.equals("")){    //无子类
                    index++;
                    results += "<li><a href='javascript:void(0)'  onclick=menuItem('"+ menu.getMenu_url() +"','"+menu.getMenu_name()+"','"+index+"') class='myMenus'><i class=\"fa "
                            + menu.getMenu_icon() + "\"></i> "
                            + menu.getMenu_name()+ "</a></li>";
                }else{    //有子类
                    if(menu.getMenu_url()==null||menu.getMenu_url().equals("")){
                        results += "<li  class=\"treeview\">"
                                + "<a href=\"javascript:void(0)\" >"
                                + "<i class=\"fa "
                                + menu.getMenu_icon()+"\"></i> <span>"
                                + menu.getMenu_name()  + "</span> <i class=\"fa fa-angle-left pull-right\"></i>"
                                + "</a>"
                                + "<ul class=\"treeview-menu\">"
                                + getBackMenuChildEntityByPID(Integer.toString(menu.getId()))
                                + "</ul>";
                    }else{
                        index++;
                        results += "<li  class=\"treeview\">"
                                + "<a href='javascript:void(0)' onclick=menuItem('"+ menu.getMenu_url() +"','"+menu.getMenu_name()+"','"+index+"')  class='myMenus'>"
                                + "<i class=\"fa "
                                + menu.getMenu_icon() +"\"></i> <span>"
                                + menu.getMenu_name()  + "</span> <i class=\"fa fa-angle-left pull-right\"></i>"
                                + "</a>"
                                + "<ul class=\"treeview-menu\">"
                                + getBackMenuChildEntityByPID(Integer.toString(menu.getId()))
                                + "</ul>";
                    }
                }
            }
        }
        return results;
    }

    public String getBackMenuHtml(List<Menu> tmpMenuS){
        this.tmpMenus = tmpMenuS;
        String resultS = getBackMenuByMenulist();
        return resultS;
    }

}
